package com.csnj.common.rest;

import com.alibaba.fastjson.JSON;
import com.csnj.common.annotation.ApiNeedLog;
import com.csnj.common.biz.BaseBiz;
import com.csnj.common.constant.CommonConstants;
import com.csnj.common.entity.APIResult;
import com.csnj.common.entity.Query;
import com.csnj.common.annotation.Resubmit;
import com.csnj.common.exception.NoPermissionException;
import com.csnj.common.exception.ParamErrorException;
import com.csnj.common.utils.BaseContextHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author xnby
 * @create 2018-11-19 8:48
 */
public abstract class BaseController<Biz extends BaseBiz,Entity> {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected Biz baseBiz;

    public Boolean checkParam(Entity entity) {
        return true;
    }
    public Boolean checkAUPermission(Entity entity) {
        return true;
    }
    public Boolean checkDPermission(Integer id) {
        return true;
    }
    public Boolean checkDPermission(String id) {
        return true;
    }

    @PostMapping("")
    @ApiNeedLog(desc = "新增")
    @Resubmit(delaySeconds = 3)
    public APIResult add(@RequestBody Entity entity){
        if(!checkAUPermission(entity)) {
            return new APIResult(new NoPermissionException(CommonConstants.MSG_PERIMISSION_ERROR));
        }
        if(!checkParam(entity)) {
            return new APIResult(new ParamErrorException(CommonConstants.MSG_PARAM_ERROR));
        }
        BaseContextHandler.set(CommonConstants.CONTEXT_KEY_PARAM, JSON.toJSONString(entity));
        baseBiz.insertSelective(entity);
        return new APIResult(entity);
    }

    @GetMapping(value = "/{id}")
    @ApiNeedLog(desc = "查询")
    public APIResult get(@PathVariable Integer id){
        Object o = baseBiz.selectById(id);
        return new APIResult(o);
    }

    @GetMapping(value = "str/{id}")
    @ApiNeedLog(desc = "查询")
    public APIResult get(@PathVariable String id){
        Object o = baseBiz.selectById(id);
        return new APIResult(o);
    }

    @PutMapping(value = "/{id}")
    @ApiNeedLog(desc = "修改")
    public APIResult update(@RequestBody Entity entity){
        if(!checkAUPermission(entity)) {
            return new APIResult(new NoPermissionException(CommonConstants.MSG_PERIMISSION_ERROR));
        }
        if(!checkParam(entity)) {
            return new APIResult(new ParamErrorException(CommonConstants.MSG_PARAM_ERROR));
        }
        BaseContextHandler.set(CommonConstants.CONTEXT_KEY_PARAM, JSON.toJSONString(entity));
        baseBiz.updateSelectiveById(entity);
        return new APIResult(entity);
    }

    @DeleteMapping(value = "/{id}")
    @ApiNeedLog(desc = "删除")
    public APIResult remove(@PathVariable Integer id){
        if(!checkDPermission(id)) {
            return new APIResult(new NoPermissionException(CommonConstants.MSG_PERIMISSION_ERROR));
        }
        baseBiz.deleteById(id);
        return new APIResult("OK");
    }

    @DeleteMapping(value = "str/{id}")
    @ApiNeedLog(desc = "删除")
    public APIResult remove(@PathVariable String id){
        if(!checkDPermission(id)) {
            return new APIResult(new NoPermissionException(CommonConstants.MSG_PERIMISSION_ERROR));
        }
        baseBiz.deleteById(id);
        return new APIResult("OK");
    }

    @GetMapping(value = "/all")
    @ApiNeedLog(desc = "查询列表")
    public APIResult all(){
        return new APIResult(baseBiz.selectListAll());
    }

    @GetMapping(value = "/page")
    @ApiNeedLog(desc = "翻页查询")
    public APIResult list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        return baseBiz.selectByQuery(query);
    }

    @GetMapping(value = "/findOne")
    @ApiNeedLog(desc="条件查询")
    public APIResult queryByEqualCondition(Entity entity) {
        return new APIResult(baseBiz.selectList(entity));
    }
}
